# -*- coding: utf-8 -*-
"""
Created on Tue Apr 04 07:50:40 2017

@author: Tubin Huang
"""
import csv
import pydatabase
import pyCSV

def createFW():
    return Framework()
    
class Framework(object):
    def setCSVLocalPath( self, localPath ):
        self.__localPath = localPath
    def setdatabase( self, datainfoDict):
        self.__datainfo = datainfoDict

def csv2DB( csvLocalPath, DBInfo, tableName, DB = pydatabase.createDatabaseWithTimestamp() ):
    with open( csvLocalPath, 'rb' ) as csvfile:
        dict_reader = csv.DictReader( csvfile)
        DB.setDBInfo(DBInfo)
        dicts = [ data for data in dict_reader ]
        DB.insert(tableName,dicts)
def dB2CSV( fileName,DBInfo, tableName,fields= ["*"], mode = "ab", limiter = dict(), DB = pydatabase.createDatabaseWithTimestamp()):
    DB.setDBInfo(DBInfo)
    _rowDicts,_fields = DB.select(tableName, fields, limiter)
    pyCSV.dict2CSV( fileName, _fields, _rowDicts, mode = mode)
def createCSVHeaderBy():
    pass
            
            
    
        

def test():
    DBInfo =  {'user':'root','password':'root','host':'localhost','database':'test'}
#    csvLocalPath = "d:\\python\\input.csv"
#    csv2DB( csvLocalPath, DBInfo , 'test' )
    dB2CSV("d:\\python\\output.csv",DBInfo,'test',fields =  ['id',"name","age"],mode="ab")

if __name__ == "__main__":
    test()
    
        